const puppeteer = require('puppeteer');
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;

// const mongo_url = "mongodb://egg_cnode:bojun1234@124.223.34.189:27127/egg_cnode?authSource=egg_cnode";
const mongo_url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@81.68.180.162:27017/taosha";


const fs = require('fs');

let totalBlogs = [];
let errLogs = [];

async function main() {
  browser = await puppeteer.launch({
    headless: true,
    ignoreHTTPSErrors: true,
    args: ['--no-sandbox', `--proxy-server=http://u5329.b5.t.16yun.cn:6460`]
  });
  const page = (await browser.pages())[0];
  await page.authenticate({'username': '16GADNHJ', 'password': '936441'});
  await page.setViewport({width: 1500, height: 0});

  const client = await MongoClient.connect(mongo_url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');

  let tab = 'today';
  const source = '淘沙日报' + (new Date(+new Date() + 1000 * 3600 * 8)).toISOString().split('T')[0];

  let totalTopics = [];
  let url = `https://hao.360.com/histoday/`;
  await page.goto(url, {waitUntil: 'domcontentloaded'});

  await page.waitForSelector('.tih-item');

  const eles = await page.$$('.tih-item');

  for (const el of eles) {
    await el.click();
    await page.waitForTimeout(500);
    let title = await el.$eval('dt', v => v.innerText);
    let original_url = await el.$eval('.read-btn', v => v.href);
    let intro = await el.$eval('.desc', v => v.innerText);
    let pic;
    if (await el.$('.item-img')) {
      pic = await el.$eval('.item-img', v => v.src);
    }
    let topic = {
      title: title.trim(),
      original_url: original_url.trim(),
      intro: intro.trim(),
      pic
    };
    totalTopics.push(topic);
  }



  let title = source;
  let content = '## 历史上的今天\n';
  for (const topic of totalTopics) {
    content += `### [${topic.title}](${topic.original_url})\n`;
    if (topic.pic) {
      content += `![](${topic.pic})\n`;
    }
    if (topic.intro) {
      content += `${topic.intro}\n`;
    }
    content += '***\n';
  }

  console.log(content);
};


main();


